package findKthToTail

type ListNode struct {
	Val  int
	Next *ListNode
}

func FindKthToTail(pHead *ListNode, k int) *ListNode {
	if pHead == nil {
		return nil

	}
	if k > Length(pHead) {
		return nil
	}

	if k <= 0 {
		return nil
	}
	n := Length(pHead) - k

	for n > 0 {
		pHead = pHead.Next
		n--
	}
	return pHead
}

func Length(pHead *ListNode) int {
	count := 1
	for pHead.Next != nil {
		count++
		pHead = pHead.Next
	}
	return count
}
